﻿Imports EstoqueDAL
Imports Conection
Public Class ServicoBLL
    Dim objDAL As ServicoDAL

#Region "Propriedades"
    Property ordcodigo As Integer
    Property vencodigo As Integer
    Property pro_codigo As Integer
    Property pro_descricao As String
    Property pro_precocusto As Decimal
    Property pro_precovenda As Decimal
    Property cat_codigo As Integer
    Property sta_codigo As Integer
    Property pro_duracao As DateTime
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String, status As Integer, venda As Decimal, custo As Decimal, categoria As Integer, duracao As DateTime)
        Me.pro_codigo = codigo
        Me.pro_descricao = descricao
        Me.pro_precocusto = custo
        Me.pro_precovenda = venda
        Me.sta_codigo = status
        Me.cat_codigo = categoria
        Me.pro_duracao = duracao
    End Sub
#End Region

#Region "Funções"
#Region "Inserir"
    Function inserir(usuario As Integer) As Boolean
        Try
            If (Me.valida) Then
                If (validareplica().Rows.Count = 0) Then
                    objDAL = New ServicoDAL
                    Me.pro_codigo = objDAL.inserir(Me.pro_descricao, Me.pro_precovenda, Me.pro_precocusto, Me.cat_codigo, Me.sta_codigo, Me.pro_duracao, usuario)
                Else : Throw New Exception("Serviço já cadastrado.")
                End If
            Else
                Throw New Exception("Preencha todos os campos.")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (Me.pro_codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Alterar"
    Function alterar(usuario As Integer) As Boolean
        Dim ret As Integer
        Try
            ret = -1

            If (Me.valida) Then
                If (validareplica().Rows.Count = 0) Then
                    objDAL = New ServicoDAL
                    ret = objDAL.alterar(Me.pro_codigo, Me.pro_descricao, Me.pro_precovenda, Me.pro_precocusto, Me.cat_codigo, Me.sta_codigo, Me.pro_duracao, usuario)
                Else : Throw New Exception("Serviço já cadastrado.")
                End If
            Else
                Throw New Exception("Preencha todos os campos.")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Localizar"
    Public Function localizar(descricao As String, tipo As Integer) As DataTable
        Try
            objDAL = New ServicoDAL
            objDAL.OrdCodigo = Me.ordcodigo
            objDAL.VenCodigo = Me.vencodigo
            Return objDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Validar Réplica de Categoria"
    Function validareplica() As DataTable
        Dim ret As DataTable
        Try
            ret = objfuncoes.ValidaReplica(Me.pro_descricao, Me.pro_codigo, "vw_Estservico", "pro_codigo", "pro_descricao")

        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Valida Campos obrigatórios"
    Public Function valida() As Boolean
        If Me.sta_codigo = Nothing Or Me.sta_codigo <= 0 Then Throw New Exception("Selecione o status.")
        If Me.pro_descricao.Trim = Nothing Or Me.pro_descricao.Trim = "" Then Throw New Exception("Preencha a descrição.")
        If (Me.cat_codigo = Nothing Or Me.cat_codigo <= 0) Then Throw New Exception("Selecione a categoria.")
        If Me.pro_precocusto = Nothing Or Me.pro_precocusto <= 0 Then Throw New Exception("Preencha o preço de custo.")
        If (Me.pro_precovenda = Nothing Or Me.pro_precovenda <= 0) Then Throw New Exception("Preencha o preço de venda.")
        If Me.pro_precocusto >= Me.pro_precovenda Then Throw New Exception("Preço de custo não pode ser maior que preço de venda.")
        If (Me.pro_duracao = Nothing) Then Throw New Exception("Hora inválida.")
        Return True
    End Function
#End Region

#End Region

#Region "Procedimentos"
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ServicoDAL
            tab = objDAL.buscar(codigo, 1)
            If (tab.Rows.Count > 0) Then
                Me.pro_codigo = Val(tab.Rows(0)("pro_codigo").ToString)
                Me.pro_descricao = tab.Rows(0)("pro_descricao").ToString
                If (Not tab.Rows(0)("pro_precocusto").ToString = "") Then Me.pro_precocusto = tab.Rows(0)("pro_precocusto").ToString
                If (Not tab.Rows(0)("pro_precovenda").ToString = "") Then Me.pro_precovenda = Decimal.Parse(tab.Rows(0)("pro_precovenda").ToString)
                Me.sta_codigo = Val(tab.Rows(0)("sta_codigo").ToString)
                Me.cat_codigo = Val(tab.Rows(0)("cat_codigo").ToString)
                Me.pro_duracao = FormatDateTime(tab.Rows(0)("pro_duracao").ToString, DateFormat.LongTime)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.pro_codigo = Nothing
        Me.pro_descricao = Nothing
        Me.pro_precocusto = Nothing
        Me.pro_precovenda = Nothing
        Me.sta_codigo = Nothing
        Me.cat_codigo = Nothing
        Me.pro_duracao = Nothing
        Me.objDAL = Nothing
    End Sub
#End Region
#End Region
End Class
